package com.lovo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.Service;
import com.lovo.service.ServiceService;
import com.lovo.util.MyUtils;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ServiceServiceImpl implements ServiceService {

    @Override
    public PageInfo<Service> listByPage(int pageNo, int pageSize, String serviceName, String serviceType, String serviceDate) {
        Service s = new Service();
        s.setServiceName(serviceName);
        s.setServiceType(serviceType);
        s.setServiceDate(serviceDate);

        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.ServiceMapper.findAllByServiceNameAndServiceTypeAndServiceDate";
        PageHelper.startPage(pageNo,pageSize);
        List<Service> list = sqlSession.selectList(stmt, s);
        PageInfo<Service> servicePageInfo = new PageInfo<>(list);
        sqlSession.commit();
        sqlSession.close();
        return servicePageInfo;
    }

    @Override
    public boolean insert(Service s) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.ServiceMapper.insertSelective";
        int n = sqlSession.insert(stmt, s);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean delete(int serviceId) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.ServiceMapper.deleteByPrimaryKey";
        int n = sqlSession.delete(stmt, serviceId);
        sqlSession.commit();
        sqlSession.close();

        return n>0?true:false;
    }

    @Override
    public boolean update(Service s) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.ServiceMapper.updateByPrimaryKeySelective";
        int n = sqlSession.insert(stmt, s);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }
}
